require(["config"],function () {
    require(["jquery","jquery.cookie","header"],function ($,c,h) {
       $(document).ready(function () {
            $(".top").load("publicTop.html",function () {
                h.head_load();
            })
           $(".buttom").load("publicbuttom.html")

           //1 点击图片切换
           $(".colorclass,.middle_bottom ul li").css({
               "cursor":"pointer"
           })
           $(".colorclass ul li,.middle_bottom ul li").click(function () {

               $(this).addClass("current").siblings().removeClass("current")

           })
           $(".colorclass ul li").click(function () {
               $(".middleBox img").attr("src",`images/middle${$(this).index()+1}.jpg`)
               $(".bigBox img").attr("src",`images/big${$(this).index()+1}.jpg`)
           })
           $(".middle_bottom ul li").click(function () {
               $(".middleBox img").attr("src",`images/xqbig${$(this).index()+1}.jpg`)
               $(".bigBox img").attr("src",`images/xqlarge${$(this).index()+1}.jpg`)
           })
           //放大镜
            var $middlebox=$(".middleBox")
            var $middlearea=$(".middleArea")
           var $bigbox=$(".bigBox img")
           var $bigarea=$(".bigBox")
            var $marealeft
            var $mareatop


               $middlebox.mouseenter(function () {
                   // console.log($bigbox)
                   $middlearea.show()
                   $bigarea.show()
                   $middlebox.mousemove(function (e) {
                       $marealeft=e.pageX-$(".middleBox").offset().left -$middlearea.width()/2
                       $mareatop=e.pageY-$(".middleBox").offset().top -$middlearea.width()/2
                       if ($marealeft<0){
                           $marealeft=0
                       } else if(
                           $marealeft>$middlebox.width()-$middlearea.width()
                       ){
                           $marealeft=$middlebox.width()-$middlearea.width()
                       }
                       if($mareatop<0){
                           $mareatop=0
                       }else if ($mareatop>$middlebox.height()-$middlearea.height()){
                           $mareatop=$middlebox.height()-$middlearea.height()
                       }
                       $middlearea.css({
                           left:$marealeft,
                           top:$mareatop
                       })
                       $bigbox.css({
                           left:-$marealeft*($bigarea.width()/$middlearea.width()),
                           top:-$mareatop*($bigarea.width()/$middlearea.width())
                       })
                   })
                    $middlebox.mouseleave(function () {
                        $middlearea.hide()
                        $bigarea.hide()
                    })
               })

           //加入购物车功能

           //1.数量加减
           $("#reduce").click(function () {
               $buynum=$(this).next().val()
               $buynum=$buynum-1<1? 1:$buynum-1
               $(this).next().val($buynum)
           })
            $("#add").click(function () {
                $(this).prev().val($(this).prev().val()-0+1)
            })
           //点击加入按钮
           $("#addclass").click(function () {
               if (sessionStorage.getItem("userinfo")){
                   var $pid=$(".pid span").text()
                   var $buynum=$(".buy_num input").val()-0
                   var $goodinfo=new Object()
                   $goodinfo["pid"]=$pid
                   $goodinfo["num"]=$buynum
                   $goodinfo["price"]=$("#price").text()-0
                   $goodinfo["title"]=$(".pname p").text()
                   $goodinfo["img"]=$(".current img").attr("src")
                   $goodinfo["uname"]=JSON.parse(sessionStorage.getItem("userinfo")).uname
                   // console.log($(".current img").attr("src"))


                   $(this).attr("data-info",JSON.stringify($goodinfo))
                   console.log($(this).data("info"))
                   var $currentdata=$(this).data("info")
                   console.log($currentdata)
                   $.ajax({
                       // url:"http://127.0.0.1/BangG_Net/server/addcar.php",
                       url:"../../server/addcar.php",
                       data:$currentdata,
                       type:"get",
                       dataType:"json"
                   }).then(function (res) {
                       if (res.status==1){
                           if (confirm("加入购物车成功!是否立即结算?")){
                               window.location.assign("goodscar.html")
                           }else{

                           }
                       }
                   })
               } else{
                   if ($.cookie("goodinfo")){
                       //存在cookie 修改数量
                       var $goodinfo=JSON.parse($.cookie("goodinfo"))
                       $goodinfo.num+=$(".buy_num input").val()-0
                       $.cookie("goodinfo",JSON.stringify($goodinfo))


                   } else {
                       var $pid=$(".pid span").text()
                       var $buynum=$(".buy_num input").val()-0
                       var $goodinfo=new Object()
                       $goodinfo["pid"]=$pid
                       $goodinfo["num"]=$buynum
                       $goodinfo["price"]=$("#price").text()-0
                       $goodinfo["title"]=$(".pname p").text()
                       $goodinfo["img"]=$(".current img").attr("src")
                       $.cookie("goodinfo",JSON.stringify($goodinfo))
                   }


                   if (confirm("加入购物车成功,是否理解结算?")){

                       window.location.assign("goodscar.html")
                   }
                   var $goodinfo=JSON.parse($.cookie("goodinfo"))
                   $("#carnum").text($goodinfo.num)

               }


           })

       })
    })
})